package com.tanhua.server.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.dubbo.api.db.*;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private BlackListApi blackListApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private UserApi userApi;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询设置
     * @return
     */
    public SettingsVo querySettings() {
        User loginUser = UserHolder.getUser();
        Settings settings = settingsApi.findByUserId(loginUser.getId());
        Question question = questionApi.findByUserId(loginUser.getId());

        SettingsVo vo = new SettingsVo();
        vo.setPhone(loginUser.getMobile());
        vo.setStrangerQuestion(null != question ? question.getTxt() : "你喜欢我吗？");
        if (null != settings) {
            BeanUtils.copyProperties(settings, vo);
        }
        return vo;
    }

    /**
     * 保存设置
     * @param vo
     */
    public void saveSetting(SettingsVo vo) {
        Settings settings = new Settings();
        BeanUtils.copyProperties(vo, settings);
        settings.setUserId(UserHolder.getUserId());
        settingsApi.save(settings);
    }

    /**
     * 分页查询黑名单
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult<UserInfoVoAge> blackListPage(Long page, Long pageSize) {
        PageResult pageResult = blackListApi.findPage(UserHolder.getUserId(), page, pageSize);
        List<BlackList> blackLists = (List<BlackList>) pageResult.getItems();
        List<UserInfoVoAge> voList = new ArrayList<>();

        if(CollectionUtils.isNotEmpty(blackLists)) {
            List<Long> userIds = blackLists.stream().map(BlackList::getBlackUserId)
                    .collect(Collectors.toList());

            List<UserInfo> userList = userInfoApi.getBatchIds(userIds);
            for (UserInfo userInfo : userList) {
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(userInfo, vo);
                //vo.setId(userInfo.getId());
                voList.add(vo);
            }
        }

        pageResult.setItems(voList);
        return pageResult;
    }

    /**
     * 验证码校验
     * @param code
     * @return
     */
    public Boolean checkVerificationCode(String code) {
        User user = new User();
        user.setId(UserHolder.getUserId());
        user = userApi.find(user);
        String key = "MODIFY_CODE_" + user.getMobile();
        String redisCode = redisTemplate.opsForValue().get(key);

        if(StringUtils.isEmpty(redisCode)) {
            throw new TanHuaException(ErrorResult.loginError());
        }

        if(!StringUtils.equals(redisCode, code)) {
            throw new TanHuaException(ErrorResult.validateCodeError());
            //return false;
        }

        redisTemplate.delete(key);
        return true;
    }

    /**
     * 修改手机号
     * @param phone
     */
    public void updateMobile(String phone) {
        User user = new User();
        user.setId(UserHolder.getUserId());
        user.setMobile(phone);
        userApi.update(user);
    }
}
